package com.idefav.controller;

import com.idefav.mapper.UserMapper;
import com.idefav.pojo.CommonResult;
import com.idefav.pojo.TdUser;
import com.idefav.pojo.TdUserdetail;
import com.idefav.pojo.User;
import com.idefav.service.UserService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.print.attribute.standard.DateTimeAtCompleted;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wzs on 2017/7/15.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/list")
    public String index(Model model) {
        model.addAttribute("title", "首页");
        model.addAttribute("users", userService.getAllUser());
        return "index";
    }

    @RequestMapping("/detail/{uid}")
    public String user_detail(Model model, @PathVariable("uid") String uid) {

        TdUser user = userService.getUserById(Integer.parseInt(uid));
        model.addAttribute("user", user);
        model.addAttribute("title", user.getUsername() + " 详情");
        return "detail";
    }

    @RequestMapping("/add")
    public String user_add(Model model) {
        model.addAttribute("title", "新增用户");
        return "adduser";
    }

    @RequestMapping(value = "/add/submit", method = RequestMethod.POST)
    public ModelAndView user_add_submit(TdUser user, TdUserdetail userdetail) {
        user.setCreatetime(DateTime.now().toDate());
        user.setUpdatetime(DateTime.now().toDate());
        user.setLastlogintime(DateTime.now().toDate());
        user.setIsdel((byte) 0);

        userdetail.setCreatetime(DateTime.now().toDate());
        userdetail.setUpdatetime(DateTime.now().toDate());
        userdetail.setIsdel((byte) 0);

        int uid=userService.getUid();
        user.setUid(uid);
        userdetail.setUid(uid);
        boolean result2 = userService.addUserDetail(userdetail);

        boolean result = userService.addUser(user);
        if (!result ||!result2) {
            Map<String,Object> data=new HashMap<>();
            data.put("message","新增失败");
            return new ModelAndView("error",data);
        }
        Map<String,Object> map = new HashMap<>();

        map.put("user",userService.getUserById(user.getUid()));
        return new ModelAndView("detail",map);
    }

    @RequestMapping(value = "/json",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult index_test() {

        return CommonResult.ok("新增");
    }

    @RequestMapping("/remove/{uid}")
    @ResponseBody
    public CommonResult user_remove(@PathVariable("uid") String uid){
        boolean b = userService.removeUser(Integer.parseInt(uid));
        if(b){
            return CommonResult.ok();
        }
        return CommonResult.build(500,"删除失败");
    }
}
